<template>
    <div class="product-list-wrap">
      <div style="position: fixed;left: 0;top: 0;width: 100%;z-index: 1000;background: #fff;">
        <header class="product-header">
          <i class="iconfont icon-back" @click="goBack"></i>
          <div class="header-search">
            <i class="iconfont icon-sousuo"></i>
            <input type="text" class="search-title" @mouseenter="textEnter" @mouseleave="textLeave" v-model="keyword">
          </div>
          <i class="iconfont icon-more" v-if="!searchBtn"></i>
          <span class="search-btn"  @click="getSearch" v-else>搜索</span>
        </header>
<!--        中间排序-->
        <section class="select-menu">
          <div class="select-item" :class="{'active' : orderBy==='default'}" data-orderBy="default"
          @click="selectOrder($event)">
            默认排序
          </div>
          <div class="select-item" :class="{'active' : orderBy==='price_asc'}" data-orderBy="price_asc"
          @click="selectOrder($event)">
            升序
            <i class="iconfont icon-up1" :class="{'active' : orderBy==='price_asc'}"></i>
          </div>
          <div class="select-item" :class="{'active' : orderBy==='price_desc'}" data-orderBy="price_desc"
          @click="selectOrder($event)">
            降序<i class="iconfont icon-down1" :class="{'active' : orderBy==='price_desc'}"></i>
          </div>
          <div class="select-item"  >筛选
            <i class="iconfont icon-shaixuan" ></i>
          </div>
        </section>
        <section class="product-list" v-show="!isLoading">
          <div>
            <div class="product-item" v-for="(item,index) in productList" @click="productDetail(index)">
              <img :src="item.imageHost+item.mainImage" v-if="item.imageHost && item.mainImage">
              <img src="../../assets/iPhoneXs.png" v-else/>
              <div class="product-info">
                <p class="name">{{item.name}}</p>
                <p class="subtitle">{{item.subtitle}}</p>
                <span class="price">￥{{item.price}}</span>
              </div>
            </div>
          </div>
        </section>

      </div>
    </div>
</template>
<script>
  import {productListKeyword,productListCategoryId} from "../../service/getData";
  import {getUrlKey,getStore,dedupeObject} from "../../common/js/util";

  export default {
    name: "product-list",
    data(){
      return{
        keyword:'',
        searchBtn:false,
        orderBy:'default',
        isLoading:true,
        params:{
          categoryId:'',
          keyword:'',
          pageNum:1,
          pageSize:20,
          orderBy:'default',
          // productList:[]
          // isLoading:false
        }
      }
    },

    // 获取商品列表（根据关键词和类别）

    // 对页面初始化

    created(){
      this.init()
    },

    methods:{
      productDetail(index) {
        this.$router.push('./product/' + this.productList[index].id)
      },

      // 价格排序
      selectOrder(e){
        let orderBy=e.currentTarget.getAttribute('data-orderBy');
        if(orderBy===this.orderBy){return}
        this.productList=[]
        this.orderBy=orderBy
        this.params.orderBy=orderBy
        this.params.pageNum=1
        this.isLoading=true
        this.getProductList(this.params)
      },
      // 获取商品
      getProductList(params){
        if (!this.keyword){
          productListCategoryId(params).then((res)=>{
            this.isLoading=false;
            if (res.data.list.length===0){
             //this.showMessage();
              this.$el.querySelector('.loading_text').style.display='none';
             this.showMessage();
              return
            }
            this.productList=this.productList.concat(res.data.list)
          })
        }  else {
          productListKeyword(params).then((res)=>{
            this.isLoading=false;
            // if (res.data.list.length===0){
            //
            //   this.$el.querySelector('.loading_text').style.display='none';
            //  this.showMessage();
            //   return
            // }
            this.productList=this.productList.concat(res.data.list)
          })
        }
      },
      getSearch(){
        this.$router.push('/product-list?keyword='+this.keyword+'&categoryId=0')//路由
        this.params.keyword=this.keyword;
        this.params.pageNum=1;
        this.productList=[];
        this.getProductList(this.params)//调用搜索方法
      },
      init(){
        let keyword = getUrlKey('keyword'),
          categoryId=getUrlKey('categoryId')
        this.productList=[]
        this.params.categoryId=categoryId
        this.params.keyword=keyword
        this.keyword=keyword
        this.getProductList(this.params)
      },

      //输入状态
      textEnter(){
        this.searchBtn=true;
      },
      //非输入状态时
      textLeave(){
        this.searchBtn=false;
      },
      //返回上一级
      goBack(){
        console.log(111);
        this.$router.go(-1);
      },


    }

  }
</script>

<style scoped lang="scss">
 @import "../../common/style/mixin";
  .product-header{
    @include fj;
    width: 100%;
    height: 100px;
    line-height: 100px;
    padding: 0 30px;
    @include boxSizing;
    font-size: 30px;
    color: #656771;
    z-index: 10000;
    &.active{
      background: $red;
    }
    .icon-left{}
    .header-search{
      display: flex;
      width: 76%;
      height: 40px;
      line-height: 40px;
      margin: 20px 0;
      padding: 10px 0;
      color: #232326;
      background: #f7f7f7;
      @include borderRadius(40PX);
      .icon-sousuo{
        padding: 0 10px 0 40px;
        font-size: 34px;
      }
      .search-title{
        font-size: 24px;
        color: #666;
        background: #f7f7f7;
      }
    }
    .icon-more{
      font-size: 40px;
    }
    .search-btn{
      height: 70px;
      margin: 15px 0;
      line-height: 70px;
      padding: 0 10px;
      color: deepskyblue;
      /*<!--background: $red;-->*/
      @include  borderRadius(10px);
    }
  }
/*排序*/
  .select-menu{
    display: flex;
    justify-content: space-around;
    width: 100%;
    height: 100px;
    background: #fff;
    &.isFixed{
      position: fixed;
      left: 0;
      top: 0;
    }
    .select-item{
      flex: 1;
      height: 100%;
      text-align: center;
      line-height: 100px;
      font-size: 30px;
      border-top: 1px solid#dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      &.active{
        color: $red;
      }
      .iconfont{
        &.icon-down1{
          color: #999;
          padding-left: 10px;
          font-size: 22px;
        }
        &.icon-up1{
          @extend  .icon-down1;
          font-size: 22px;
        }
        &.icon-shaixuan{
          @extend .icon-down1;
          font-size: 22px;
        }
        &.active{
          color: $red;
        }
      }
    }
  }

  /*排序结果*/
  .product-list{
    width: 100%;
    margin-top: 10px;
    .product-item{
      @include fj;
      width: 100%;
      height: 240px;
      padding: 20px 0;
      border-bottom: 1px solid #dcdcdc;
      img{
        width: 280px;
        height: 240px;
        padding: 0 20px;
        @include boxSizing;
      }
      .product-info{
        width: 56%;
        height: 240px;
        padding: 10px;
        @include boxSizing;
        .name{
          margin-top: 20px;
          width: 100%;
          max-height: 80px;
          line-height: 40px;
          font-size: 30px;
          color: #333;
          overflow: hidden;
        }
        .subtitle{
          width: 100%;
          max-height: 40px;
          padding: 20px 0;
          line-height: 50px;
          font-size: 26px;
          color: #999;
          overflow: hidden;
        }
        .price{
          color: $red;
          font-size: 32px;
        }
      }
    }
  }
</style>
